<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="560px"
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="mealModel" :rules="formRules"
      ref="foodBankForm" label-width="80px" label-position="right" size="small">
      <el-form-item label="编码" prop="code">
        <el-col :span="16"><el-input ref="code" v-model="mealModel.code" autofocus></el-input></el-col>
      </el-form-item>
      <el-form-item label="食品名称" prop="name">
        <el-col :span="16"><el-input  v-model="mealModel.name"></el-input></el-col>
      </el-form-item>
      <el-form-item label="所属类型" prop="typeid">
        <el-col :span="16">
          <el-select  v-model="mealModel.typeid">
              <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="100g含能量" prop="energy">
        <el-col :span="13"><el-input  v-model="mealModel.energy"></el-input></el-col>
      </el-form-item>
      <el-form-item label="蛋白质含量" prop="protein">
        <el-col :span="13"><el-input  v-model="mealModel.protein"></el-input></el-col>
      </el-form-item>
      <el-form-item label="脂肪含量" prop="fat">
        <el-col :span="13"><el-input  v-model="mealModel.fat"></el-input></el-col>
      </el-form-item>


    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('foodBankForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {  updateTMeals,saveTMeals,listTMealsType } from '@/api/xsf/foodBank'

export default {
  name: 'role-edit',
  data() {
    return {
      loading: false,
      mealModel: {
        id: 0,
        code: '',
        name:'',
        typeid:0,
        energy:0,
        fat:0,
        protein:0
      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      formRules: {
        code: [
          { required: true, message: '请输入编码', trigger: 'blur' },
        ],
        name: [
          { required: true, message: '请输入食物名称', trigger: 'blur' },
        ],
        energy: [
          { required: true, message: '请输入能量', trigger: 'blur' },
        ],
        fat: [
          { required: true, message: '请输入脂肪含量', trigger: 'blur' },
        ],
        protein: [
          { required: true, message: '请输入蛋白质含量', trigger: 'blur' },
        ]
      },
      options:[]

    }
  },
  methods: {

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSaveRole()
          } else {
            this.doUpdateRole()
          }
        } else {
          return false
        }
      });
    },
    doUpdateRole() {
      this.loading = true
      updateTMeals(this.mealModel.id, this.mealModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)
      })
    },
    doSaveRole() {
      this.loading = true
      saveTMeals(this.mealModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)
      })
    },
    handleDialogClose() {
      this.dialogProps.visible = false
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['foodBankForm'].clearValidate()
        this.$refs['code'].focus()
      })
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })
    },
    initSelectOption(){
      listTMealsType().then((response)=>{
        this.options=response.data;
      })
    },
  },
  mounted: function() {
    this.initSelectOption();

    this.$nextTick(() => {
      this.$on('openEditRoleDialog', function(role) {
        this.mealModel = role
        this.$set(this.mealModel,'typeid',Number.parseInt(role.typeid))

        this.dialogProps.action = 'edit'
        this.dialogProps.title = '修改膳食库'
        this.dialogProps.visible = true
      })
      this.$on('openAddRoleDialog', function() {
        this.dialogProps.action = 'add'
        this.dialogProps.title = '添加膳食库'
        this.mealModel = {
          locked: false
        }
        this.dialogProps.visible = true
      })
    })
  }
}
</script>

